package com.binfenhulian.chinaenergy.ces.crawler.processor.seedlinked;

import com.alibaba.fastjson.JSON;
import com.binfenhulian.chinaenergy.ces.crawler.bean.enums.CrawlerMatchTypeEnum;
import com.binfenhulian.chinaenergy.ces.crawler.bean.param.NextRegexBean;
import com.binfenhulian.chinaenergy.ces.crawler.bean.param.NextRegexItemArgsBean;
import com.binfenhulian.chinaenergy.ces.crawler.bean.param.NextRegexItemBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * 从html层组装url
 * Created by wanghjbuf on 2018/3/13.
 */
@Slf4j
@Component("com.binfenhulian.chinaenergy.ces.crawler.processor.seedlinked.SeedLinkedFromCombProcessor")
public class SeedLinkedFromCombProcessor implements PageProcessor {
    private String crawlerNo;
    private String destContentXpath;
    private String destUrlXpath;
    private NextRegexBean nextRegexBean;

    /**
     * 初始化处理器
     * @param crawlerNo
     * @param nextRegex
     * @param destContentXpath
     * @param destUrlXpath
     */
    public void  initPageProcessor(String crawlerNo, String nextRegex, String destContentXpath, String destUrlXpath) {
        this.crawlerNo = crawlerNo;
        this.destContentXpath = destContentXpath;
        this.destUrlXpath = destUrlXpath;
        this.nextRegexBean = JSON.parseObject(nextRegex, NextRegexBean.class);
    }

    /**
     * 执行解析
     * @param page
     */
    @Override
    public void process(Page page) {
        // TODO Auto-generated method stub
        List<String> listUrls = page.getHtml().xpath(this.destUrlXpath).links().all();
        List<String> listContents = page.getHtml().xpath(this.destContentXpath).all();

        page.putField("crawler_no", this.crawlerNo);
        page.putField("list_url", listUrls);
        page.putField("list_content", listContents);

        List<NextRegexItemBean> ruleItemList = nextRegexBean.getRules();
        switch (CrawlerMatchTypeEnum.getCrawlerMatchTypeEnum(nextRegexBean.getType())) {
            case REGEX :
                ruleItemList.forEach(ruleItem ->{
                    List<NextRegexItemArgsBean> itemList = ruleItem.getItems();
                    itemList.forEach(item -> {
                        page.addTargetRequests(page.getHtml().links().regex(item.getMainRule()).all());
                    });
                });

                break;
            case XPATH:
                List<String> targetRequestList = new ArrayList<>();
                ruleItemList.forEach(ruleItem ->{
                    List<NextRegexItemArgsBean> itemList = ruleItem.getItems();
                    //TODO 暂时仅支持一个参数
                    itemList.forEach(item -> {
                        List<String> values = page.getHtml().xpath(item.getMainRule()).regex(item.getAssistRule()).all();
                        values.forEach(str -> {
                            StringBuffer targetRequest = new StringBuffer(ruleItem.getUrlPrefix()).append(item.getKey()).append("=").append(str);
                            targetRequestList.add(targetRequest.toString());
                        });
                    });
                });
                page.addTargetRequests(targetRequestList);

                break;
        }
    }

    /* (non-Javadoc)
     * @see us.codecraft.webmagic.processor.PageProcessor#getSite()
     */
    @Override
    public Site getSite() {
        // TODO Auto-generated method stub
        return Site.me().setRetrySleepTime(3)
                .setSleepTime(10000)
                .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");

    }
}
